//
//  MXMineController.m
//  HNBaseProject
//
//  Created by xujie on 2024/5/31.
//

#import "MXMineController.h"
#import "MXMineCommonV.h"
#import "MXMineUserInfoController.h"
#import "MXServiceController.h"
#import "MXFeedBackController.h"
#import "MXHomeListController.h"
#import "DCWebViewController.h"
#import "DCNavigationController.h"
#import "MXLoginController.h"
@interface MXMineController ()
@property (nonatomic, strong) UILabel *nameLab;
@end

@implementation MXMineController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor dc_colorWithHexString:@"#ebf4f8"];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateUserName) name:@"updatenicknamesuccess" object:nil];
    [self createUI];
}

- (void)createUI {
    MXUserInfoModel *model = [DCCacheTool getCurrentUserInfo];
    self.nameLab = [[UILabel alloc] init];
    self.nameLab.text = model.name;
    self.nameLab.textColor = [UIColor colorWithHexString:@"#000000"];
    self.nameLab.font = PFRFontMedium(23);
    [self.view addSubview:self.nameLab];
    [self.nameLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(25);
        make.top.offset(kNavBarHeight);
        make.right.offset(-50);
    }];
    
    MJWeakSelf
    UIImageView *editImg = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"mine_edit"]];
    editImg.userInteractionEnabled = YES;
    [editImg addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithActionBlock:^(id  _Nonnull sender) {
        [weakSelf gotoEdit];
    }]];
    [self.view addSubview:editImg];
    [editImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.offset(-22);
        make.height.width.mas_equalTo(21);
        make.centerY.mas_equalTo(self.nameLab);
    }];
    
    UIView *bgView = [[UIView alloc] init];
    bgView.userInteractionEnabled = YES;
    bgView.backgroundColor = [UIColor whiteColor];
    bgView.layer.cornerRadius = 11;
    bgView.layer.masksToBounds = YES;
    [self.view addSubview:bgView];
    [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(15);
        make.right.offset(-13);
        make.height.mas_equalTo(53);
        make.top.mas_equalTo(self.nameLab.mas_bottom).offset(41);
    }];
    
    UIView *bgViewB = [[UIView alloc] init];
    bgViewB.userInteractionEnabled = YES;
    bgViewB.backgroundColor = [UIColor whiteColor];
    bgViewB.layer.cornerRadius = 11;
    bgViewB.layer.masksToBounds = YES;
    [self.view addSubview:bgViewB];
    [bgViewB mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(15);
        make.right.offset(-13);
        make.height.mas_equalTo(113);
        make.top.mas_equalTo(bgView.mas_bottom).offset(20);
    }];
    
    MXMineCommonV *shareManageV = [[MXMineCommonV alloc] init];
    [shareManageV reloadViewWithIcon:@"mine_share" name:@"共享管理"];
    [bgView addSubview:shareManageV];
    [shareManageV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(16);
        make.right.offset(-11);
        make.height.mas_equalTo(19);
        make.centerY.mas_equalTo(bgView);
    }];
    
//    [shareManageV addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithActionBlock:^(id  _Nonnull sender) {
//        [weakSelf shareManage];
//    }]];
    
    UIButton *shareManageBtn = [[UIButton alloc] init];
    [shareManageV addSubview:shareManageBtn];
    [shareManageBtn addTarget:self action:@selector(shareManage) forControlEvents:UIControlEventTouchUpInside];
    [shareManageBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.bottom.offset(0);
    }];
    
    MXMineCommonV *serviceV = [[MXMineCommonV alloc] init];
    [serviceV reloadViewWithIcon:@"mine_service" name:@"喵享家客服"];
//    [serviceV addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithActionBlock:^(id  _Nonnull sender) {
//        [weakSelf tapService];
//    }]];
    
    UIButton *serviceVBtn = [[UIButton alloc] init];
    [serviceV addSubview:serviceVBtn];
    [serviceVBtn addTarget:self action:@selector(tapService) forControlEvents:UIControlEventTouchUpInside];
    [serviceVBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.bottom.offset(0);
    }];
    
    [bgViewB addSubview:serviceV];
    
    MXMineCommonV *feedV = [[MXMineCommonV alloc] init];
    [feedV reloadViewWithIcon:@"mine_feedback" name:@"意见反馈"];
    
    UIButton *feedVBtn = [[UIButton alloc] init];
    [feedV addSubview:feedVBtn];
    [feedVBtn addTarget:self action:@selector(tapFeedBack) forControlEvents:UIControlEventTouchUpInside];
    [feedVBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.bottom.offset(0);
    }];
    [bgViewB addSubview:feedV];
    
    [serviceV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(16);
        make.top.offset(19);
        make.right.offset(-11);
        make.height.mas_equalTo(19);
    }];
    
    [feedV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(16);
        make.bottom.offset(-25);
        make.right.offset(-11);
        make.height.mas_equalTo(19);
    }];
    
    UIView *bgViewC = [[UIView alloc] init];
    bgViewC.userInteractionEnabled = YES;
    bgViewC.backgroundColor = [UIColor whiteColor];
    bgViewC.layer.cornerRadius = 11;
    bgViewC.layer.masksToBounds = YES;
    [self.view addSubview:bgViewC];
    [bgViewC mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(15);
        make.right.offset(-13);
        make.height.mas_equalTo(139);
        make.top.mas_equalTo(bgViewB.mas_bottom).offset(20);
    }];
    
    MXMineCommonV *protV = [[MXMineCommonV alloc] init];
    [protV reloadViewWithIcon:@"mine_user_protcol" name:@"用户协议"];
    
    UIButton *protVBtn = [[UIButton alloc] init];
    [protV addSubview:protVBtn];
    [protVBtn addTarget:self action:@selector(tapProt) forControlEvents:UIControlEventTouchUpInside];
    [protVBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.bottom.offset(0);
    }];
    [bgViewC addSubview:protV];
    
    [protV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(16);
        make.top.offset(19);
        make.right.offset(-11);
        make.height.mas_equalTo(19);
    }];

    MXMineCommonV *policyV = [[MXMineCommonV alloc] init];
    [policyV reloadViewWithIcon:@"mine_policy" name:@"隐私政策"];
    
    UIButton *policyBtn = [[UIButton alloc] init];
    [policyV addSubview:policyBtn];
    [policyBtn addTarget:self action:@selector(tapPolicy) forControlEvents:UIControlEventTouchUpInside];
    [policyBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.bottom.offset(0);
    }];
    [bgViewC addSubview:policyV];
    
    MXMineCommonV *ruleV = [[MXMineCommonV alloc] init];
    [ruleV reloadViewWithIcon:@"mine_rule" name:@"社区规则"];
    
    UIButton *ruleBtn = [[UIButton alloc] init];
    [ruleV addSubview:ruleBtn];
    [ruleBtn addTarget:self action:@selector(tapRule) forControlEvents:UIControlEventTouchUpInside];
    [ruleBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.bottom.offset(0);
    }];
    [bgViewC addSubview:ruleV];
    
    [protV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(16);
        make.top.offset(19);
        make.right.offset(-11);
        make.height.mas_equalTo(19);
    }];
    
    [policyV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(16);
        make.centerY.mas_equalTo(bgViewC);
        make.right.offset(-11);
        make.height.mas_equalTo(19);
    }];
    
    [ruleV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(16);
        make.bottom.offset(-19);
        make.right.offset(-11);
        make.height.mas_equalTo(19);
    }];
    
    UILabel *logOutLab = [[UILabel alloc] init];
    logOutLab.backgroundColor = UIColor.whiteColor;
    logOutLab.userInteractionEnabled = YES;
    logOutLab.textAlignment = NSTextAlignmentCenter;
    logOutLab.text = @"退出登录";
    logOutLab.layer.cornerRadius = 17;
    logOutLab.layer.masksToBounds = YES;
    logOutLab.layer.borderWidth = 1;
    logOutLab.layer.borderColor = [UIColor colorWithHexString:@"#ECECEC"].CGColor;
    logOutLab.textColor = [UIColor colorWithHexString:@"#000000"];
    logOutLab.font = PFRFontSemibold(16);
    [self.view addSubview:logOutLab];
    [logOutLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.view);
        make.height.mas_equalTo(34);
        make.width.mas_equalTo(147);
        make.top.mas_equalTo(bgViewC.mas_bottom).offset(30);
    }];
    
    
    UILabel *canleLab = [[UILabel alloc] init];
    canleLab.backgroundColor = UIColor.whiteColor;
    canleLab.userInteractionEnabled = YES;
    canleLab.textAlignment = NSTextAlignmentCenter;
    canleLab.text = @"注销账号";
    canleLab.layer.cornerRadius = 17;
    canleLab.layer.masksToBounds = YES;
    canleLab.layer.borderWidth = 1;
    canleLab.layer.borderColor = [UIColor colorWithHexString:@"#ECECEC"].CGColor;
    canleLab.textColor = [UIColor colorWithHexString:@"#000000"];
    canleLab.font = PFRFontSemibold(16);
    [self.view addSubview:canleLab];
    [canleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.view);
        make.top.mas_equalTo(logOutLab.mas_bottom).offset(15);
        make.height.mas_equalTo(34);
        make.width.mas_equalTo(147);
    }];
    
    [logOutLab addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithActionBlock:^(id  _Nonnull sender) {
        [weakSelf logOut];
    }]];
    
    [canleLab addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithActionBlock:^(id  _Nonnull sender) {
        [weakSelf deleteAccount];
    }]];
   
}

- (void)deleteAccount {
    MJWeakSelf
    [self.view makeToastActivity];
   [[DCHttpClient shareClient] requestWithPath:@"api/logoff" params:@{} httpMethod:DCHttpRequestPost sucess:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull responseObject) {
        NSDictionary * dict = [responseObject mj_JSONObject];
        [weakSelf.view hideToastActivity];
        if (dict[DC_ResultCode_Key] && [dict[DC_ResultCode_Key] integerValue] == DC_Result_Success) {
            
            MXUserInfoModel *model = [[MXUserInfoModel alloc] init];
            [DCCacheTool saveUserInfo:model];
            
            UIWindow *window = [weakSelf getKeyWindow];
            window.rootViewController = [[DCNavigationController alloc] initWithRootViewController:[[MXLoginController alloc] init]];
            [window makeKeyAndVisible];
        } else {

            [weakSelf.view xjmakeToast:dict[DC_ResultMsg_Key]];
        }
    } failture:^(NSURLSessionDataTask * _Nonnull task, NSError * _Nonnull error) {
        [weakSelf.view hideToastActivity];
    }];
}

- (void)logOut {
    
     MJWeakSelf
     [self.view makeToastActivity];
    [[DCHttpClient shareClient] requestWithPath:@"api/quit" params:@{} httpMethod:DCHttpRequestPost sucess:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull responseObject) {
         NSDictionary * dict = [responseObject mj_JSONObject];
         [weakSelf.view hideToastActivity];
         if (dict[DC_ResultCode_Key] && [dict[DC_ResultCode_Key] integerValue] == DC_Result_Success) {
             
             MXUserInfoModel *model = [[MXUserInfoModel alloc] init];
             [DCCacheTool saveUserInfo:model];
             
             UIWindow *window = [weakSelf getKeyWindow];
             window.rootViewController = [[DCNavigationController alloc] initWithRootViewController:[[MXLoginController alloc] init]];
             [window makeKeyAndVisible];
         } else {

             [weakSelf.view xjmakeToast:dict[DC_ResultMsg_Key]];
         }
     } failture:^(NSURLSessionDataTask * _Nonnull task, NSError * _Nonnull error) {
         [weakSelf.view hideToastActivity];
     }];

    
}

- (UIWindow *)getKeyWindow {
    
    UIWindow *keyWindow;
    if (@available(iOS 13.0, *)) {
        
        for (UIWindowScene* windowScene in [UIApplication sharedApplication].connectedScenes) {
            if (windowScene.activationState == UISceneActivationStateForegroundActive) {
                
                keyWindow = windowScene.windows.firstObject;
                break;
            }
        }
    }
    return keyWindow;
}

//社区规则
- (void)tapRule {
    [self gotoWebViewWithStr:@"https://www.maoxga.cn/shequ.html" title:@"喵享家社区规则"];
}

//隐私政策
- (void)tapPolicy {
    [self gotoWebViewWithStr:@"https://www.maoxga.cn/yinsi.html" title:@"隐私协议"];
}

//用户协议
- (void)tapProt {
    [self gotoWebViewWithStr:@"https://www.maoxga.cn/xieyi.html" title:@"用户协议"];
}

- (void)gotoWebViewWithStr:(NSString *)urlStr title:(NSString *)title {
    DCWebViewController *webVc = [[DCWebViewController alloc] initWithUrl:urlStr];
    webVc.titleString = title;
    [self.navigationController pushViewController:webVc animated:YES];
}

//编辑
- (void)gotoEdit {
    MXMineUserInfoController *infoVc = [[MXMineUserInfoController alloc] init];
    [self.navigationController pushViewController:infoVc animated:YES];
}

//共享管理
- (void)shareManage {
    MXHomeListController *listVc = [[MXHomeListController alloc] init];
    [self.navigationController pushViewController:listVc animated:YES];
}

//客服
- (void)tapService {
    MXServiceController *serviceVc = [[MXServiceController alloc] init];
    [self.navigationController pushViewController:serviceVc animated:YES];
}
//意见反馈
- (void)tapFeedBack {
    MXFeedBackController *Vc = [[MXFeedBackController alloc] init];
    [self.navigationController pushViewController:Vc animated:YES];
}

- (void)updateUserName {
    MXUserInfoModel *model = [DCCacheTool getCurrentUserInfo];
    self.nameLab.text = model.name;
}

-(void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

@end
